import Dexie from 'dexie'
Dexie.debug = process.env.NODE_ENV !== 'production'
//只在渲染进程中使用,也就是浏览器端使用
export let db = null
let dexieId = window.localStorage.getItem("userId") || '00000'
// uuid 字段这里作为会话窗口字段, 
export const dexieInit = async (userId) => {
  if (!userId) return
  if (dexieId === userId && db) return db
  db = new Dexie('IM_' + userId)
  dexieId = userId
  db.version(1).stores({
    conversation: `++id,timestamp,nickName,userId,portrait,userType,lastMsg,hasRead,msgType,top,disturb,
  msgCustomType,groupId,groupName,groupPortrait,uuid,msgId`,
    privateChat: `++id,timestamp,nickName,userId,portrait,userType,msgType,top,disturb,content,uuid,msgId`,
    groupChat: `++id,timestamp,nickName,userId,portrait,userType,msgType,top,disturb,content,
  groupId,groupName,groupPortrait,uuid,msgId`,
    groupInfo: `++id,uuid,groupId,friendEach,master,administrator,alertSubdivideType`,
  })

  // id  uuid 每个群的唯一id 服务端群id 全员禁言 是否允许互加好友 是否开启二维码  entryMode入群方式 master 是否是群主  administrator 是否是管理员
  //  keepGroup  是否保存到群聊 personBanned 是否被管理员禁言
  await db.open()
}
dexieInit(window.localStorage.getItem("userId") || 'IM_00000')
